﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.Services.DataCache;
using Enter.Services.Mail;
using Enter.Mvc.Base.Controllers;
using System.Web.Mvc;
using Enter.Mvc.Mall.Model.Goods;
using Enter.Config;
using Enter.Mvc.Mall.Model.User;
using System.Data;
using Enter.Mvc.Mall.Model.Product;
using Enter.Common;
using Newtonsoft.Json;
using Enter.Mvc.Mall.Model;
using Enter.Entity;

namespace Enter.Mvc.Mall.Controllers
{
  public  class goodsController : BaseControllers
    {
         private readonly ICache icache;
        private readonly IMail imail;

        public goodsController(ICache cache, IMail mail)
        {
            this.icache = cache;
            this.imail = mail;
        }
        public ActionResult goods_in_stock()
        {
           int banner= RequestStrHelper.GetIntFromRequest("banner");
           ViewData["banner"] = banner;
            return View();
        }

        //public ActionResult goods_in_stock(int banner)
        //{
        //    ViewData["banner"] = "banner";
        //    return View();
        //}
        public ActionResult GetGoods(GoodSearthModel model)
        {
             string CateNO = model.Category;// "01010201";
       
            string title = model.title;
            decimal sprice = model.searchPriceFrom;
            decimal eprice = model.searchPriceTo;
               int startnum = model.searchNumFrom;
            int endnum = model.searchNumTo;
            string outerId=model.outerId;
              string classNo=model.Category;
            EnumGoodOrder orderCol=model.orderField;
            EnumOrderType orderType=model.orderBy;
      string shopNo=UserInfo.CurrentUser.ShopNo;
            int pagesize = 10;
            int currentIndex = model.currentIndex;
            int totalPages, totalCount;
            EnumGoodStatus status = (EnumGoodStatus)model.banner;
         DataTable dt= imail.GetWaitGoodsByShop(shopNo, status, classNo, title, outerId, sprice, eprice, startnum, endnum, orderCol, orderType, pagesize, currentIndex, out totalPages, out totalCount);

         List<GoodModel> list = new List<GoodModel>();
         if (dt != null)
         {
             foreach (DataRow dr in dt.Rows)
             {
                 GoodModel good = new GoodModel();
                 good.GID = int.Parse(dr["GID"].ToString());
                 good.No = dr["GNo"].ToString();
                 good.Title = dr["GTitle"].ToString();
                 good.ImgUrl = ImgPathConfig.productInfoServerPath + dr["SmallImgUrl"].ToString();
                 good.STOCKS = SafeConvert.ToInt32(dr["GSTOCKS"].ToString());
                 good.SaleCount = SafeConvert.ToInt32(dr["SaleCount"].ToString());
                 good.AddTime = SafeConvert.ToDateTime(dr["GAddtime"]).ToString("yyyy-MM-dd HH:mm");
                 good.EndTime = SafeConvert.ToDateTime(dr["GEndTime"]).ToString("yyyy-MM-dd HH:mm");
                 good.SalePRICE = SafeConvert.ToDecimal(dr["GSalePRICE"].ToString());
                 list.Add(good);
             }
         }
         object obj = new { list = list, totalPages = totalPages, currentIndex = currentIndex };
         string result = JavaScriptConvert.SerializeObject(obj);
         return Content(result);
        }
        public ActionResult SaveTitle(int id,string title)
        {
            var results = new JsonResultModel();
            try
            {
                if (imail.UpdateTitle(id, UserInfo.CurrentUser.ShopNo, title))
                {
                    results.ResultCode = "00";
                    results.ResultMsg = "修改成功";
                }
                else {
                    results.ResultCode = "01";
                    results.ResultMsg = "修改失败";
                }
            }
            catch (Exception ex)
            {
                results.ResultCode = "01";
                results.ResultMsg = ex.Message;
            }
            return Json(results);
        }
        public ActionResult edit_price_SKU_frame(int itemId)
        {
         T101_GoodsEntity goodinfo= imail.GetProduct(itemId);
         if (goodinfo.GShopNO == UserInfo.CurrentUser.ShopNo)
         {
             List<T103_Goods_ItemEntity> list = imail.getGoodItem(goodinfo.GNo);
             List<Goods_ItemModel> items = null;
             if (list != null)
             {
                 items = new List<Goods_ItemModel>();
                 foreach (T103_Goods_ItemEntity e in list)
                 {
                     Goods_ItemModel m = new Goods_ItemModel();
                     m.ID = e.ID;
                     m.ColorName = e.ColorName;
                     m.SizeName = e.SizeName;
                     m.PRICE = e.PSalePRICE;
                     items.Add(m);
                 }
             }
             return View(items);
         }
         return View();
        }
    }
}
